*===============================================================================
*
*					WORKER BELIEFS ABOUT OUTSIDE OPTIONS
*		(c)	Simon Jaeger, Christopher Roth, Nina Roussille, Benjamin Schoefer
*							  2023 December 5
*						   	   SOEP-IAB Data 
*
*===============================================================================


********************************************************************************
*								Figure E01	 								   *
********************************************************************************

cap log close                        
log using ${log}/FigureE01.log, replace     

foreach restriction in "main" "all" "med" "2017mov" "20mov" "socc" "sinc" "sage" "sedu" "nz"{
	
	* main specification - robustness tests change one of these attributes
	local year "2015" 
	local stat "mean" 
	local samp "FTp_u_"
	
	if "`restriction'"=="all" local samp "FTp_"  // all movers 
	if "`restriction'"=="med" local stat "med" // median mover wage change
	if "`restriction'"=="2017mov" local year "2017" // only 2017-2019 movers 
	if "`restriction'"=="20mov" local samp "FTp_m20_" // 20+ movers
	if "`restriction'"=="socc" local samp "FTp_so_" // same occupation movers
	if "`restriction'"=="sinc" local samp "FTp_si_" // similar income movers
	if "`restriction'"=="sage" local samp "FTp_sa_" // same age movers
	if "`restriction'"=="sedu" local samp "FTp_se_" // same education movers
	if "`restriction'"=="nz" local samp "nz_" // excluding same wage beliefs

	use "$data/`samp'_finaldata.dta", clear	
		
		
	*** final variable preparation
	* winsorizing 'coworker wage changes' variable
	cap drop actual_delta_ln_mover_change 
	winsor `stat'_delta_ln_wage_mover_2015, gen(actual_delta_ln_mover_change) p(0.02) 

	* winsorizing empirical bayes variable 
	if "`restriction'"!="med"{		
		cap drop ebayes_delta_ln_mover_change
		winsor ebayes_delta_ln_wage_mover_2015, gen(ebayes_delta_ln_mover_change) p(0.02)
	}

	* winsorizing split sample IV variable
	cap drop actual_delta_ln_mover_change_r0 
	winsor `stat'_delta_ln_wage_mover_r0_2015, gen(actual_delta_ln_mover_change_r0) p(0.02) 
	cap drop actual_delta_ln_mover_change_r1 
	winsor `stat'_delta_ln_wage_mover_r1_2015, gen(actual_delta_ln_mover_change_r1) p(0.02) 

	
	*** no adjustments 
	reg ln_own_change_w actual_delta_ln_mover_change, cluster(pid)
	local N_obs = e(N)
	local beta = string(round(_b[actual_delta_ln_mover_change],0.001),"%4.3f")
	local se = string(round(_se[actual_delta_ln_mover_change],0.001),"%4.3f")
	local constant = _b[_cons]
	local se_constant = _se[_cons]
	
	* locals for het-figure 
	matrix regtab=r(table)
	local cil_ols_`restriction' = string(round(regtab[5,1],0.001),"%4.3f")
	local cih_ols_`restriction' = string(round(regtab[6,1],0.001),"%4.3f")
	local bet_ols_`restriction' = string(round(_b[actual_delta_ln_mover_change],0.001),"%4.3f")


	*** split sample IV 
	ivregress 2sls ln_own_change_w (actual_delta_ln_mover_change_r1 = actual_delta_ln_mover_change_r0),  cluster(pid)	
	local N_obs_IV = e(N)
	local beta_IV = string(round(_b[actual_delta_ln_mover_change_r1],0.001),"%4.3f")
	local se_IV = string(round(_se[actual_delta_ln_mover_change_r1],0.001),"%4.3f")
	
	* locals for het-figure 
	matrix regtab=r(table)
	local cil_iv_`restriction' = string(round(regtab[5,1],0.001),"%4.3f")
	local cih_iv_`restriction' = string(round(regtab[6,1],0.001),"%4.3f")
	local bet_iv_`restriction' = string(round(_b[actual_delta_ln_mover_change_r1],0.001),"%4.3f")	

	
	*** EB
	if "`stat'"=="mean" { // not for median
		reg ln_own_change_w ebayes_delta_ln_mover_change, cluster(pid)
		* locals for het-figure 
		matrix regtab=r(table)
		local cil_eb_`restriction' =string(round(regtab[5,1],0.001),"%4.3f")
		local cih_eb_`restriction' =string(round(regtab[6,1],0.001),"%4.3f")
		local bet_eb_`restriction' = string(round(_b[ebayes_delta_ln_mover_change],0.001),"%4.3f")
	}
	else{
		local cil_eb_`restriction' = .
		local cih_eb_`restriction' = .
		local bet_eb_`restriction' = .
	}
	
}

* ML on 2015-2019 or 2018-2019 movers 
use "$data/FTp_u__finaldata.dta", clear	
foreach year in "" "1819"{
	
	reg ln_own_change_w p_delta_ln_wage_mover`year', cluster(pid)
	local N_obs = e(N)
	local beta = string(round(_b[p_delta_ln_wage_mover`year'],0.001),"%4.3f")
	local se = string(round(_se[p_delta_ln_wage_mover`year'],0.001),"%4.3f")
	local constant = string(round(_b[_cons],0.001),"%4.3f")
	cap local se_constant = _se[_cons]
	
	* locals for het-figure 
	matrix regtab=r(table)
	local cil_ols_ml`year' = string(round(regtab[5,1],0.001),"%4.3f")
	local cih_ols_ml`year' = string(round(regtab[6,1],0.001),"%4.3f")
	local bet_ols_ml`year' = string(round(_b[p_delta_ln_wage_mover`year'],0.001),"%4.3f")
	local cil_eb_ml`year' = .
	local cih_eb_ml`year' = .
	local bet_eb_ml`year' = .
	local cil_iv_ml`year' = .
	local cih_iv_ml`year' = .
	local bet_iv_ml`year' = .
}


global types ols iv eb // estimation strategies
global hetgroups "main" "all" "med" "2017mov" "20mov" "socc" "sinc" "sage" "sedu" "nz" "ml" "ml1819" // different samples 
local num : list sizeof global(hetgroups)

* create matrices 
foreach type in $types{
	matrix beta_`type' = J(1,`num',.)
	matrix rownames beta_`type' = Estimates
	matrix CI_`type' = J(2,`num',.)
	matrix rownames CI_`type' = ll95 ul95
}

* store information in matrices
foreach type in $types{
	local i 0
	foreach hetvar in $hetgroups {
		local ++ i	
			matrix beta_`type'[1, `i'] = `bet_`type'_`hetvar''
			matrix CI_`type'[1, `i'] = `cil_`type'_`hetvar''
			matrix CI_`type'[2, `i'] = `cih_`type'_`hetvar''
	}
}

coefplot(matrix(beta_ols), ci(CI_ols) ciopts(lc(maroon) lw(0.3)) mcolor(maroon) m(S) msize(0.75) label(Unadjusted, Belief Own Wage Change)) ///
        (matrix(beta_eb), ci(CI_eb) ciopts(lc(cranberry) lw(0.3)) mcolor(cranberry) m(T) msize(0.75) label(Empirical Bayes, ))  ///
        (matrix(beta_iv), ci(CI_iv) ciopts(lc(gs9) lw(0.3)) mcolor(gs9) m(D) msize(0.75) label(Split Sample IV, ))  ///	
		, xline(1, lp(solid) lc(gs6)) xline(0, lp(dash) lc(gs6)) xscale(r(-0.25 1)) yscale(r(0.2 12.7) lstyle(none))  ///
		legend(order(2 "Unadjusted" 4 "Empirical Bayes" 6 "Split Sample IV") cols(1) ring(0) pos(5) size(3.5) bmargin(tiny) region(color(white%0))) ///
		ylabel(1 "Main Specification (EUE Movers)" 2 "All Movers" 3 "Median Mover Wage Change" 4 "Only 2017-2019 Movers" 5 "20+ Movers" 6 "Same Occupation Movers" 7 "Similar Income Movers" 8 "Same Age Movers" 9  "Same Education Movers" 10 "Excluding Same Wage Beliefs" 11 "ML on 2015-2019 Movers" 12 "ML on 2018-2019 Movers", labsize(3.5)) xlabel(-0.2 "-.2" 0 "0" 0.2 ".2" 0.4 ".4" 0.6 ".6" 0.8 ".8" 1 "1", nogrid labsize(3.5)) xtitle("Slope Coefficients: Belief vs. Benchmark") 
		 addplot: scatteri 3 0.975 "Accurate Beliefs: Slope = 1", msymbol(i) mlabpos(0) mlabcolor(black) mlabangle(90) legend(order(2 "Unadjusted" 4 "Empirical Bayes" 6 "Split Sample IV") cols(1) ring(0) pos(5) size(3.5) bmargin(tiny) region(color(white%0))) ylabel(1 "Main Specification (EUE Movers)" 2 "All Movers" 3 "Median Mover Wage Change" 4 "Only 2017-2019 Movers" 5 "20+ Movers" 6 "Same Occupation Movers" 7 "Similar Income Movers" 8 "Same Age Movers" 9  "Same Education Movers" 10 "Excluding Same Wage Beliefs" 11 "ML on 2015-2019 Movers" 12 "ML on 2018-2019 Movers", labsize(3.5)) xlabel(-0.2 "-.2" 0 "0" 0.2 ".2" 0.4 ".4" 0.6 ".6" 0.8 ".8" 1 "1", nogrid labsize(3.5)) xtitle("Slope Coefficients: Belief vs. Benchmark") 

graph display ,	xsize(30) ysize(17.5)
graph export "${log}/FigureE01.pdf", replace

									
			
log close 
clear
				
						
						